<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * pendidikan.php
 * @author ranuandra
 *
 * 7 Mei 2012 13:06:14
 *
 * 
 */


class Controller_Pendidikan extends Kohana_Nasmoco
{
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$datas = $pelamar->pelamarpendidikan->find_all();
		$view = View::factory('pendidikan/index');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_search()
	{
		$search = $_GET['q'];
		if($search != "")
		{
			$datas = ORM::factory('pelamarpendidikan')->where('institusi', 'like', $search.'%')->find_all();
			$result = "";
			foreach($datas as $data)
			{
				$result .= $data->institusi ."|".$data->institusi."\n";
			}
			
			echo $result;
		}
	}
	
	public function action_add()
	{
		$pendidikan = ORM::factory('mstpendidikan')->find_all()->as_array('id', 'pendidikan');
		$jurusan = ORM::factory('mstjurusan')->find_all()->as_array('id', 'jurusan');
		$universitases = ORM::factory('mstuniversitas')->find_all()->as_array('id', 'universitas');
		$view = View::factory('pendidikan/form');
		$view->pendidikanlist = $pendidikan;
		$view->universitases = $universitases;
		$view->jurusanlist = $jurusan;
		$this->template->container = $view;
	}
	
	public function action_save()
	{
		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();
			
			try 
			{
				
				if(isset($_POST['id']))
				{
					$pendidikan = ORM::factory('pelamarpendidikan', Request::current()->post('id'));
					$pendidikan->mstpendidikan_id = Request::current()->post('mstpendidikan_id');
					$pendidikan->mstjurusan_id = Request::current()->post('mstjurusan_id');
					$pendidikan->thn_lulus = Request::current()->post('thn_lulus');
					$pendidikan->type_pendidikan = Request::current()->post('type_pendidikan');
					$pendidikan->institusi = Request::current()->post('institusi');
					$pendidikan->save();
				}
				else 
				{
					$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
					$pendidikan = ORM::factory('pelamarpendidikan');
					$pendidikan->mstpelamar_id = $pelamar->id;
					$pendidikan->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}
}